<template>
  <div>
    <el-form ref="formRef" :model="formData" :rules="rules" label-postion="right" label-width="100px">
      <el-form-item label="网站名称" prop="webName">
        <el-input placeholder="网站名称" v-model="formData.webName"></el-input>
      </el-form-item>
      <el-form-item label="站长" prop="nickname">
        <el-input placeholder="站长" v-model="formData.nickname"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input placeholder="邮箱" v-model="formData.email"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input placeholder="手机号" v-model="formData.phone"></el-input>
      </el-form-item>
      <el-form-item label="开发人员">
        <el-input type="textarea" :row="2" placeholder="开发人员，多个人员之间用逗号隔开"
                  v-model="formData.devName"></el-input>
      </el-form-item>
      <el-form-item label="版权所有">
        <el-input type="textarea" :row="2" placeholder="版权所有" v-model="formData.copy"></el-input>
      </el-form-item>
      <el-form-item label="网站介绍1">
        <el-input type="textarea" :row="2" placeholder="网站介绍1" v-model="formData.profile1"></el-input>
      </el-form-item>
      <el-form-item label="网站介绍2">
        <el-input type="textarea" :row="2" placeholder="网站介绍2" v-model="formData.profile2"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="formData.status">
          <el-radio :label="1">可用</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="logo" prop="logo">
        <el-upload :action="uploadUrl" :headers="uploadHeader" :limit="1" :file-list="logoFileList"
                   list-type="picture-card" ref="uploadLogo" :on-exceed="onExceed" :on-success="logoSuccess"
                   :before-upload="beforeUpload">
          <i class="el-icon-plus"></i>
          <div slot="file" slot-scope="{file}">
            <img :src="file.url" alt="" class="el-upload-list__item-thumbnail">
            <div class="el-upload-list__item-actions">
          <span @click="showImage(file)">
            <i class="el-icon-zoom-in"></i>
          </span>
              <span @click="removeImage(file,'uploadLogo')">
            <i class="el-icon-delete"></i>
          </span>
            </div>
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item label="二维码" prop="qrCode">
        <el-upload :action="uploadUrl" :headers="uploadHeader" :limit="1" :file-list="qrCodeFileList"
                   list-type="picture-card" ref="uploadQrCode" :on-exceed="onExceed" :on-success="qrCodeSuccess"
                   :before-upload="beforeUpload">
          <i class="el-icon-plus"></i>
          <div slot="file" slot-scope="{file}">
            <img :src="file.url" alt="" class="el-upload-list__item-thumbnail">
            <div class="el-upload-list__item-actions">
          <span @click="showImage(file)">
            <i class="el-icon-zoom-in"></i>
          </span>
              <span @click="removeImage(file,'uploadQrCode')">
            <i class="el-icon-delete"></i>
          </span>
            </div>
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate">提交</el-button>
      </el-form-item>
    </el-form>
    <el-dialog title="查看图片" :visible.sync="imageDialogVisible" v-if="imageDialogVisible" append-to-body>
      <img :src="showImageSrc" alt="" height="100%" width="100%">
    </el-dialog>
  </div>
</template>

<script>
const formData = {
  webName: '',
  logo: '',
  nickname: '',
  profile1: '',
  profile2: '',
  id: '',
  devName: '',
  copy: '',
  email: '',
  phone: '',
  qrCode: '',
  status: 1
}
export default {
  name: 'WebsiteUpdate',
  data () {
    return {
      formData: { ...formData },
      rules: {
        webName: [{
          required: true,
          message: '网站名称不能为空',
          trigger: 'blur'
        }],
        nickname: [{
          required: true,
          message: '站长不能为空',
          trigger: 'blur'
        }]
      },
      uploadUrl: process.env.VUE_APP_API + 'admin/upload',
      uploadHeader: {
        token: ''
      },
      logoFileList: [],
      qrCodeFileList: [],
      showImageSrc: '',
      imageDialogVisible: false
    }
  },
  methods: {
    init () {
      this.logoFileList.length = 0
      this.qrCodeFileList.length = 0
      this.$http.get('admin/website/info').then(data => {
        this.formData = { ...data.data }
        this.logoFileList.push({ url: process.env.VUE_APP_API + 'img/' + data.data.logo })
        this.qrCodeFileList.push({ url: process.env.VUE_APP_API + 'img/' + data.data.qrCode })
      })
    },
    onExceed () {
      this.$message.error('请先删除原图片')
    },
    logoSuccess (res) {
      if (res.code === 200) {
        this.formData.logo = res.fileName
      } else {
        this.$message.error(res.msg)
      }
    },
    qrCodeSuccess (res) {
      if (res.code === 200) {
        this.formData.qrCode = res.fileName
      } else {
        this.$message.error(res.msg)
      }
    },
    beforeUpload (file) {
      const isImage = file.type === 'image/jpeg' || file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/gif'
      if (!isImage) {
        this.$message.error('请选择正确的文件格式')
      }
      const isLt20M = (file.size / 1024 / 1024) < 20
      if (!isLt20M) {
        this.$message.error('文件大小不能超过20MB')
      }
      return isImage && isLt20M
    },
    removeImage (file, ref) {
      this.$confirm('确定删除该图片吗？').then(() => {
        this.$refs[ref].handleRemove(file)
        if (ref === 'uploadLogo') {
          this.formData.logo = ''
        } else {
          this.formData.qrCode = ''
        }
      })
    },
    showImage (file) {
      this.imageDialogVisible = true
      this.showImageSrc = file.url
    },
    saveOrUpdate () {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.$http.post('admin/website/update', this.formData).then(() => {
            this.$alert(`${this.formData.id ? '修改' : '添加'}成功！`, { type: 'success' }).then(() => {
              this.init()
            })
          })
        }
      })
    }
  },
  created () {
    this.uploadHeader.token = this.$cookies.get('token')
    this.init()
  }
}
</script>

<style scoped>

</style>
